<template>
  <div class="MyAdd">
    <nav-bar title="我的地址" />

    <!-- 显示收货地址 -->
    <div class="address-view">
      <div :key="index" class="address-card" v-for="(address, index) in addressList" >
        <div class="address-card-select">
          <i class="fa fa-check-circle" v-if="selectIndex === index" />
        </div>
        <div @click="selectAddress(address, index)" class="address-card-body">
          <p class="address-card-title">
            <span class="username">{{ address.name }}</span>
            <span class="gender" v-if="address.sex">{{ address.sex }}</span>
            <span class="phone">{{ address.phone }}</span>
          </p>
          <p class="address-card-address">
            <span class="tag" v-if="address.tag">{{address.tag}}</span>
            <span class="address-text">{{ address.address }} {{ address.bottom }}</span>
          </p>
        </div>
        <div class="address-card-edit">
          <i @click="editAddress(address)" class="fa fa-edit" />
          <i @click="deleteAddress(address, index)" class="fa fa-close" />
        </div>
      </div>
    </div>

    <div class="btm" @click="$router.push('/addaddress')">
      <i class="fa fa-plus-circle"></i>
      <span>新增收货地址</span>
    </div>
  </div>
</template>

<script>
import NavBar from "./NavBar";
export default {
  name: "MyAddress",
  components: {
    NavBar
  },
  data() {
    return {
      addressList: [],
      selectIndex: 0
    };
  },
  activated() {
    axios("/api/user/user_info/" + localStorage.getItem("token")).then(res => {
      this.addressList = res.myAddress;
    });
  },
  methods: {
    selectAddress(item, index) {
      this.selectIndex = index;
      this.$store.dispatch('setMyAddress',{item})
      this.$router.push('/settlement')
    },
    // 修改地址
    editAddress(address) {
      this.$router.replace({
        path: "/addAddress",
        // 这边首先将对象转换成字符串,然后进行字符串编码
        query: {
          title: "修改地址",
          address: encodeURIComponent(JSON.stringify(address))
        }
      });
    },
    // 删除地址
    deleteAddress(address, index) {
      axios
        .delete(`/api/user/address/${localStorage.token}/${address._id}`)
        .then(res => {
           this.addressList.splice(index, 1);
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>

<style scoped>
.tag {
  color: red;
  border: 1px solid red;
  font-size: 12px;
  padding: 1px;
}
.MyAdd {
  height: 100vh;
}
.btm {
  position: fixed;
  bottom: 0;
  left: 0;
  height: 50px;
  width: 100%;
  border-top: 1px solid #ccc;
  line-height: 50px;
  text-align: center;
  color: #3190e8;
  font-size: 17px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btm > span {
  margin-left: 5px;
}
.btm > i {
  font-size: 30px;
}
.myAddress {
  width: 100%;
  height: 100%;
  padding-top: 54px;
}

.address-view-bottom {
  font-size: 16px;
  position: fixed;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 50px;
  color: #3190e8;
  border-top: 1px solid #dddddd;
  background: #ffffff;
}

.address-view-bottom > i {
  font-size: 30px;
  margin-right: 8px;
}

.address-view {
  overflow-y: auto;
  height: 100%;
  padding-bottom: 60px;
}

.address-card {
  display: flex;
  min-height: 60px;
  padding: 20px 15px;
  border-bottom: 1px solid #dddddd;
  background-color: #ffffff;
}

.address-card-body {
  overflow: hidden;
  flex: 1;
}

.address-card-title {
  font-size: 16px;
  display: flex;
  overflow: hidden;
  align-items: center;
  padding-bottom: 10px;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #666666;
}

.address-card-title .username {
  font-weight: 700;
  color: #333333;
}

.address-card-title .gender {
  font-size: 14px;
  display: inline-block;
  margin-right: 10px;
  margin-left: 10px;
  padding: 4px;
  white-space: nowrap;
  color: #ff5722;
  border: 1px solid #ff5722;
  border-radius: 3px;
}

.address-card-address {
  font-size: 14px;
  display: flex;
  align-items: center;
  color: #666666;
}

.address-text {
  line-height: 20px;
  max-width: 220px;
}

/* 编辑和删除 */
.address-card-edit {
  display: flex;
  align-items: center;
  flex-basis: 50px;
  justify-content: space-between;
}

.address-card-edit > i:first-child {
  font-size: 20px;
  display: inline-block;
  color: #aaaaaa;
}

.address-card-edit > i:last-child {
  font-size: 22px;
  display: inline-block;
  padding-bottom: 3px;
  color: #aaaaaa;
}

/* 选中图标 */
.address-card-select {
  display: flex;
  align-items: center;
  width: 30px;
}

.address-card-select > i {
  font-size: 20px;
  color: #4cd964;
}
</style>
</style>